package com.ffyc.backserver.common;


import org.springframework.web.servlet.HandlerInterceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/*
    后端验证用户是否登录
 */
public class LoginInterceptor implements HandlerInterceptor {

    /*
       预处理--进入到控制器之前执行
       返回false 不会进入到控制器
       返回true 进入到控制
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token =   request.getHeader("token");
        boolean res =  JWTUtil.verify(token);
        if(!res){//res值为false, 向前端响应一个401状态码
            response.getWriter().print(401);
        }
        return res;
    }


}
